// (CreateWindowsTileImages.jsx) Photoshop Script to Create Windows Tile images
//
// by Toine de Boer | Enbyin / Aviva Solutions (http://www.avivasolutions.nl)
//
try {
    var artwork = File.openDialog("Select a sqaure PSD file that is 1240x1240 to get the best result.", "*.psd", false);

    if (artwork !== null) {
        var doc = open(artwork);

        if (doc == null) {
            throw "Something is wrong with the file";
        }

        var startState = doc.activeHistoryState; // save for undo
        var initialPrefs = app.preferences.rulerUnits; // will restore at end
        app.preferences.rulerUnits = Units.PIXELS; // use pixels

        var destFolder = Folder.selectDialog("Choose an output folder");

        if (destFolder == null) {
            // user canceled
            throw "";
        }

        // Save icons in PNG using 'Save for Web'
        var sfw = new ExportOptionsSaveForWeb();
        sfw.format = SaveDocumentType.PNG;
        sfw.PNG8 = false; // use PNG-24
        sfw.transparency = true; // transpart for the edges
        doc.info = null; // delete metadata

        var icons = [  // when (h == 0 or null) the width size will be taken 

		  { "w": 284, "name": "Square71x71Logo.scale-400" },
		  { "w": 142, "name": "Square71x71Logo.scale-200" },
		  { "w": 107, "name": "Square71x71Logo.scale-150" },
		  { "w": 89, "name": "Square71x71Logo.scale-125" },
		  { "w": 71, "name": "Square71x71Logo.scale-100" },


		  { "w": 600, "name": "Square150x150Logo.scale-400" },
		  { "w": 300, "name": "Square150x150Logo.scale-200" },
		  { "w": 225, "name": "Square150x150Logo.scale-150" },
		  { "w": 188, "name": "Square150x150Logo.scale-125" },
		  { "w": 150, "name": "Square150x150Logo.scale-100" },


		  { "w": 1240, "h": 600, "name": "Wide310x150Logo.scale-400" },
		  { "w": 620, "h": 300, "name": "Wide310x150Logo.scale-200" },
		  { "w": 465, "h": 225, "name": "Wide310x150Logo.scale-150" },
		  { "w": 388, "h": 188, "name": "Wide310x150Logo.scale-125" },
		  { "w": 310, "h": 150, "name": "Wide310x150Logo.scale-100" },


		  { "w": 1240, "h": 0, "name": "Square310x310Logo.scale-400" },
		  { "w": 620, "name": "Square310x310Logo.scale-200" },
		  { "w": 465, "name": "Square310x310Logo.scale-150" },
		  { "w": 388, "name": "Square310x310Logo.scale-125" },
		  { "w": 310, "name": "Square310x310Logo.scale-100" },


		  { "w": 176, "name": "Square44x44Logo.scale-400" },
		  { "w": 88, "name": "Square44x44Logo.scale-200" },
		  { "w": 66, "name": "Square44x44Logo.scale-150" },
		  { "w": 55, "name": "Square44x44Logo.scale-125" },
		  { "w": 44, "name": "Square44x44Logo.scale-100" },

		  { "w": 256, "name": "Square44x44Logo.targetsize-256" },
		  { "w": 256, "name": "Square44x44Logo.targetsize-256_altform-unplated" },
		  { "w": 48, "name": "Square44x44Logo.targetsize-48" },
		  { "w": 48, "name": "Square44x44Logo.targetsize-48_altform-unplated" },
		  { "w": 24, "name": "Square44x44Logo.targetsize-24" },
		  { "w": 24, "name": "Square44x44Logo.targetsize-24_altform-unplated" },
		  { "w": 16, "name": "Square44x44Logo.targetsize-16" },
		  { "w": 16, "name": "Square44x44Logo.targetsize-16_altform-unplated" },


		  { "w": 200, "name": "StoreLogo.scale-400" },
		  { "w": 100, "name": "StoreLogo.scale-200" },
		  { "w": 75, "name": "StoreLogo.scale-150" },
		  { "w": 63, "name": "StoreLogo.scale-125" },
		  { "w": 50, "name": "StoreLogo.scale-100" },

		  { "w": 96, "name": "BadgeLogo.scale-400" },
		  { "w": 48, "name": "BadgeLogo.scale-200" },
		  { "w": 36, "name": "BadgeLogo.scale-150" },
		  { "w": 30, "name": "BadgeLogo.scale-125" },
		  { "w": 24, "name": "BadgeLogo.scale-100" },

		  { "w": 2480, "h": 1200, "name": "SplashScreen.scale-400" },
		  { "w": 1240, "h": 600, "name": "SplashScreen.scale-200" },
		  { "w": 930, "h": 450, "name": "SplashScreen.scale-150" },
		  { "w": 775, "h": 375, "name": "SplashScreen.scale-125" },
		  { "w": 620, "h": 300, "name": "SplashScreen.scale-100" }
        ];

        var icon;
        var filename = doc.name.substr(0, doc.name.lastIndexOf('.'));

        for (i = 0; i < icons.length; i++) {
            icon = icons[i];

            var destFileName = "";

            if ((icon.h == null || icon.h == 0)) {
                destFileName = filename + icon.w + ".png";
                icon.h = icon.w;
            }
            else {
                destFileName = filename + icon.w + "x" + icon.h + ".png";
            }

            if (icon.name != null && icon.name != "") {
                destFileName = icon.name + ".png";
            }

            if (icon.h != icon.w) {
                // get smallest value
                var smallestSize = icon.h;
                if (icon.w < icon.h) {
                    smallestSize = icon.w;
                }

                doc.resizeImage(smallestSize, smallestSize, // width, height
                    null, ResampleMethod.BICUBICSHARPER);

                doc.resizeCanvas(icon.w, icon.h);

                // (with more than 1)
                // highest layer must be the background....
                if (doc.layers.length > 1) {
                    var bgLayer = doc.layers[doc.layers.length - 1];
                    bgLayer.resize((doc.width.value / (bgLayer.bounds[2] - bgLayer.bounds[0])) * 100, (doc.height.value / (bgLayer.bounds[3] - bgLayer.bounds[1])) * 100, AnchorPosition.MIDDLECENTER);
                }
            }
            else {
                doc.resizeImage(icon.w, icon.h, // width, height
                    null, ResampleMethod.BICUBICSHARPER);
            }

            doc.exportDocument(new File(destFolder + "/" + destFileName), ExportType.SAVEFORWEB, sfw);
            doc.activeHistoryState = startState; // undo resize
        }

        alert("Images created and ready to copy and paste.");
        alert("Now go and drink some coffee to spend the time you just saved with this script. ");

    }
}
catch (exception) {
    if ((exception != null) && (exception != ""))
        alert(exception);
}
finally {
    if (doc != null)
        doc.close(SaveOptions.DONOTSAVECHANGES);

    app.preferences.rulerUnits = initialPrefs;
}

